/**
 * @author Linrui
 * @since 2011-05-06
 * 校园信息管理
 */
package com.qeado.company.web;

import java.io.File;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.qeado.company.model.Company;
import com.qeado.company.service.CompanyService;
import com.qeado.news.model.News;
import com.qeado.news.service.NewsService;
import com.qeado.sort.model.Sort;
import com.qeado.sort.service.SortService;
import com.qeado.user.model.User;
import com.qeado.util.FileUpload;
import com.qeado.util.PageHelper;

@Scope("session")
@Controller
public class CompanyManageAction extends ActionSupport {

	@Autowired
	private SortService sortService;
	@Autowired
	private NewsService newsService;
	@Autowired
	private CompanyService companyService;
	
	private List<Sort> sortList; // 信息类别
	private News news;  // 新闻DTO 
	private PageHelper publicInfo; // 已发布的信息
	
	private List<Sort> industryList; // 企业类别 下拉菜单选项
	private List<Sort> industryCategoryList; // 行业类型 下 下拉菜单选项
	private List<Sort> managementModeList; // 经营模式 下拉菜单选项
	private List<Sort> bankList; // 开户银行 下拉菜单选项
	private List<Sort> qualityList; // 质量控制 下拉菜单选项
	
	private Company company; // 企业实体DTO
	
	private File myFile; // 上传的文件
	private String contentType; // 上传的文件类型
	private String fileName; // 上传文件的文件名
	
	private String pageNum = "1"; // 当前页码
	private String orderBy = "create_date";
	private String asc = "desc";
	private String pageSize = "10";
	private String urlMark = "1"; // 用于标记次链接是一个发布操作还是更新操作 1：发布 2：更新  默认是发布
 
    public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public void setMyFileContentType(String contentType) {
        this.contentType = contentType;
   } 
   
    public void setMyFileFileName(String fileName) {
        this.fileName = fileName;
   } 
       
    public void setMyFile(File myFile) {
        this.myFile = myFile;
   } 
	
	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getAsc() {
		return asc;
	}

	public void setAsc(String asc) {
		this.asc = asc;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getUrlMark() {
		return urlMark;
	}

	public void setUrlMark(String urlMark) {
		this.urlMark = urlMark;
	}

	public String getPageNum() {
		return pageNum;
	}

	public void setPageNum(String pageNum) {
		this.pageNum = pageNum;
	}

	public PageHelper getPublicInfo() {
		return publicInfo;
	}

	public void setPublicInfo(PageHelper publicInfo) {
		this.publicInfo = publicInfo;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public List<Sort> getSortList() {
		return sortList;
	}

	public void setSortList(List<Sort> sortList) {
		this.sortList = sortList;
	}

	@Override
	public String execute() throws Exception {
		User user = (User)ActionContext.getContext().getSession().get("login_user");
		if( user.getCompanyID() == null )
			return "pre_company";
		else
			return viewCompany();
	}

	/* 预先加载类别信息 */
	public String publicInfoPage() throws Exception {
		if (sortList == null)
			sortList = sortService.listByPcode("CM001");
		return "public_company_info";
	}

	/* 发布信息 */
	public String publicInfo() throws Exception {
		String statu ;
		User user = (User)ActionContext.getContext().getSession().get("login_user");
		FileUpload upload = null;
		if (myFile != null) {
			upload = new FileUpload();
			upload.setBasePath(ServletActionContext.getServletContext().getRealPath("uploadfiles"));
			upload.setUserDir(user.getId());
			upload.setMyFile(myFile);
			upload.setFileName(fileName);
		}
		news.setCreator(user.getUserName());
		statu = newsService.insertNews(news,upload);
		if( "success".equals(statu) ) {
			pageNum = "1"; // 跳转到第一页
			return listInfo();
		} else
			return "fail";
	}
	
	/* 删除发布的信息 */
	public String deleteInfo() {
		return null;
	}
	
	/* 分页查询已发布的纪录 */
	public String listInfo() throws Exception {
		String userName = ((User)ActionContext.getContext().getSession().get("login_user")).getUserName() ;
		if( userName != null && !"".equals(userName) ){
				publicInfo = newsService.listByUser(Integer.parseInt(pageNum), Integer.parseInt(pageSize), userName, orderBy, asc);
				publicInfo.setPagebar("my/listInfo?pageSize="
					+ pageSize + "&orderBy=" + orderBy + "&asc=" + asc);
				return "info_list";
		} else {
			return "fail";
		}
	}
	
	/* 创建一家公司 */
	public String createCompany() throws Exception{
		User user = (User)ActionContext.getContext().getSession().get("login_user");
		if( user.getCompanyID() != null)
			return "fail"; // 无权限访问
		FileUpload upload = null;
		if (myFile != null) {
			upload = new FileUpload();
			upload.setBasePath(ServletActionContext.getServletContext().getRealPath("uploadfiles"));
			upload.setUserDir(user.getId());
			upload.setMyFile(myFile);
			upload.setFileName(fileName);
		}
		company.setAddress(); // 设置地址
		if( "success".equals( companyService.insertCompany(company,upload,user) ) )
			return "success"; // 操作成功
		else
			return "visit_fail"; // 操作失败
	}
	
	/* 创建公司页面数据的读取 */
	public String addPage() {
		if( industryList == null )
			industryList = sortService.listByPcode("CM005"); // 企业类型列表
		if( industryCategoryList == null )
			industryCategoryList = sortService.listByPcode("CM003"); // 行业类型列表
		if( managementModeList == null )
			managementModeList = sortService.listByPcode("CM004"); // 经营模式列表
		if( bankList == null )
			bankList = sortService.listByPcode("CM002"); // 加载银行列表
		if(qualityList == null )
			qualityList = sortService.listByPcode("CM006"); // 加载质量控制列表
		/* 如果是创建操作，这直接进入添加页面 */
		if( "1".equals(urlMark) ) {
			return "add_page";
		}
		/* 如果是修改操作，先判断用户是否企业管理员，然后再判断传入ID是否有效，
		 * 然后再判断传入ID是否和用户的企业ID相同，再判断该企业信息是否已经加载
		 * 最后进入修改页面 */
		else {
			User user = (User)ActionContext.getContext().getSession().get("login_user");
			if( "0".equals(user.getAdminFlag()) || user.getAdminFlag() == null )
				return "fail"; // 无访问权限
			else {
				String id = ServletActionContext.getRequest().getParameter("id");
				if( id != null && !"".equals(id) ) {
					if( !id.equals(user.getCompanyID()))
						return "fail"; // 无权访问
					if( company == null )
						company = companyService.queryById(id);
					return "add_page";
				} else {
					return "visit_fail";
				}
			}
		}		
	}

	/* 加载企业的基本资料 */
	public String viewCompany() {
		company = companyService.queryById( ((User)ActionContext.getContext().getSession().get("login_user")).getCompanyID() );
		if( company != null )
			return "view";
		else
			return "visit_fail";
	}
	
	/* 更新企业的基本资料 */
	public String updateCompany() {
		User user = (User)ActionContext.getContext().getSession().get("login_user");
		if( "0".equals(user.getAdminFlag()) || user.getAdminFlag() == null )
			return "fail"; // 无访问权限
		if( user.getCompanyID() == null || !user.getCompanyID().equals(company.getId()))
			return "fail"; // 无权限访问
		FileUpload upload = null;
		/* 如果用户修改图片 */
		if (myFile != null) {
			upload = new FileUpload();
			upload.setBasePath(ServletActionContext.getServletContext().getRealPath("uploadfiles"));
			upload.setUserDir(user.getId());
			upload.setMyFile(myFile);
			upload.setFileName(fileName);
		}
		company.setAddress(); // 设置地址
		if ( companyService.updateCompany(company,upload)  )
				return "view"; 
		else
				return "visit_fail"; // 操作失败
	}	

	public List<Sort> getIndustryList() {
		return industryList;
	}

	public List<Sort> getIndustryCategoryList() {
		return industryCategoryList;
	}

	public List<Sort> getManagementModeList() {
		return managementModeList;
	}

	public List<Sort> getBankList() {
		return bankList;
	}

	public List<Sort> getQualityList() {
		return qualityList;
	}
	
}
